<?php
/**
 * @version $Id: view.php 22 2011-09-20 10:52:20Z truongnhan0311 $
 */

defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

class WrdView extends JViewLegacy{
	
	function getModel($name = null) {
		if (!($model = parent::getModel($name))) {
			$this->addModel($name);
			return parent::getModel($name);
		}
		else
			return $model;
	}
	
	function get($property, $default = null) {
		$args = func_get_args();
		array_shift($args);
		array_shift($args);
		
		if ($default != null)
			$this->addModel($default);
			
		// If $model is null we use the default model
		if (is_null($default)) {
			$model = $this->_defaultModel;
		} else {
			$model = strtolower( $default );
		}

		// First check to make sure the model requested exists
		if (isset( $this->_models[$model] )){
			// Model exists, lets build the method name
			$method = 'get'.ucfirst($property);

			// Does the method exist?
			if (method_exists($this->_models[$model], $method)){
				// The method exists, lets call it and return what we get
				$result = call_user_func_array(array($this->_models[$model], $method), $args);
                return $result;
			}
		}

		// degrade to JObject::get
		$result = parent::get( $property, $default );
		return $result;
			
		return parent::get($property, $default);
	}
	
	function addModel($name) {
		if (!isset($this->_models[strtolower($name)])) {
			$model = &JModelLegacy::getInstance($name, 'WrdModel');
			$this->setModel($model);
		}
	}
}
?>